package com.love.cloud.admin.controller;

import com.love.cloud.admin.api.vo.DingTalkDeptInfoVo;
import com.love.cloud.admin.api.vo.DingTalkUserInfoVo;
import com.love.cloud.admin.api.vo.DingTalkUserTreeVo;
import com.love.cloud.admin.dto.DingTalkUserTreeDto;
import com.love.cloud.admin.dto.EmployeeSearchOrgDto;
import com.love.cloud.admin.service.DingTalkService;
import com.love.cloud.admin.service.DingTalkUserTreeService;
import com.love.cloud.common.core.exception.BusinessException;
import com.love.cloud.common.core.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hongdongqu
 */
@Api(tags = "钉钉相关接口")
@RestController
@RequestMapping("/DingTalk")
public class DingTalkController { //同步锁
    private static String asyncLockKey="asyncLock";
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private DingTalkService dingTalkService;
    @Resource
    private DingTalkUserTreeService dingTalkUserTreeService;

    @GetMapping("/getZZDAppId")
    @ApiOperation(value = "获取到浙政钉的参数")
    public Object getZZDParams(){
        return dingTalkService.getZZDParams();
    }

    @PostMapping("/asyncAllData")
    @ApiOperation(value = "同步全部数据")
    public Object asyncAllData(){
        this.checkIdLock();
        dingTalkService.asyncAllData();
        return "执行同步成功，等待同步完成";
    }

    @PostMapping("/asyncDataByDeptId")
    @ApiOperation(value = "同步通过部门")
    public Object asyncDataByDeptId(Long deptId){
        this.checkIdLock();
        dingTalkService.asyncDataByDeptId(deptId);
        return "执行同步成功，等待同步完成";
    }

    @GetMapping("/getDeptAndUserInfo")
    @ApiOperation(value = "获取到浙政钉部门信息和用户信息")
    public DingTalkUserTreeDto getDeptAndUserInfo(DingTalkUserTreeVo dingTalkUserTreeVo){
        return dingTalkUserTreeService.getDeptAndUserInfo(dingTalkUserTreeVo);
    }


    @GetMapping("/searchDingTalkUserInfo")
    @ApiOperation(value = "搜索到用户信息")
    public List<EmployeeSearchOrgDto> getUserInfo(DingTalkUserInfoVo dingTalkUserInfoVo){
        return dingTalkUserTreeService.getUserInfo(dingTalkUserInfoVo);
    }

    @GetMapping("/searchDingTalkDeptInfo")
    @ApiOperation(value = "搜索到部门信息")
    public Object searchDingTalkDeptInfo(DingTalkDeptInfoVo dingTalkDeptInfoVo){
        return dingTalkUserTreeService.searchDingTalkDeptInfo(dingTalkDeptInfoVo);
    }

    @GetMapping("/getOrganizationLine")
    @ApiOperation(value = "根据组织Code查询组织汇报线")
    public Object getOrganizationLine(String organizationCode){
        return dingTalkUserTreeService.getOrganizationLine(organizationCode);
    }

    /**
     * 校验是否再同步了
     */
    private void checkIdLock(){
        Object asyncLack = redisUtils.get(asyncLockKey);
        //只允许同时有一个在同步
        if(asyncLack!=null){
            throw new BusinessException("上次钉同步还未完成");
        }
    }

}
